package cn.hxth.cloud.provider.updater;

import cn.hxth.cloud.api.common.constant.OrderInfoStatus;
import cn.hxth.cloud.api.common.util.DateProvider;
import cn.hxth.cloud.api.domain.Product;
import cn.hxth.cloud.api.domain.ProductOutputOrderItem;
import cn.hxth.cloud.api.mapper.ProductMapper;

import java.util.List;
import java.util.Map;

public class OutPutProductUpdater extends AbstractProductUpdater {
    private Map<Long,ProductOutputOrderItem> map ;
    private  Integer orderInfoStatus ;

    public OutPutProductUpdater(List<Product> products, ProductMapper productMapper, DateProvider dateProvider, Map<Long, ProductOutputOrderItem> map, Integer orderInfoStatus) {
        super(products, productMapper, dateProvider);
        this.map = map;
        this.orderInfoStatus = orderInfoStatus ;
    }


    @Override
    protected void updateLockedInventory() {
        for(Product product:products){
             ProductOutputOrderItem item = map.get(product.getId());
             if(!OrderInfoStatus.DELIVERED.equals(orderInfoStatus))
            product.setLockedInventory(product.getLockedInventory()-item.getCount());
        }
    }

    @Override
    protected void updateInventory() {
        for(Product product:products){
            ProductOutputOrderItem item = map.get(product.getId());
            product.setInventory(product.getInventory()-item.getCount());
        }
    }

}
